package com.study.services;

import java.util.HashMap;
import java.util.List;

import org.eclipse.jdt.internal.compiler.env.IModule.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.study.model.jdao.InboundJDao;
import com.study.model.mdao.InboundMapper;
import com.study.model.mdao.InventoryMapper;
import com.study.model.mdao.ProductMapper;
import com.study.model.pojo.Inbound;

@Service
public class InboundService {
	@Autowired
	InboundMapper iMapper;
	@Autowired
	ProductMapper pMapper;
	@Autowired
	InventoryMapper imMapper;
	@Autowired
	InboundJDao iDao;
	
	/**
	 * 查询所有入库信息
	 * @return
	 */
	public List<Inbound> findAll(){
		return iMapper.findAll();
	}
	
	/**
	 * 查询入库信息记录数
	 * @return
	 */
	public int findcount() {
		return iMapper.findcount();
	}
	
	/**
	 * 根据入库类别查询
	 * @param intName
	 * @return
	 */
	public List<Inbound> findByintName(String intName){
		return iMapper.findByintName(intName);
	}
	
	/**
	 * 入库
	 * @param map 入库信息
	 * @param proid 产品id
	 * @param warName 仓库名称
	 * @param inbNum 入库数量
	 */
	public String inbound(HashMap map,String proid,Inbound inbound) {
		int i=pMapper.findexist(proid);//是否存在
		String returnString;
		if (i==1) {
			iMapper.addInbound(map);//新增入库信息	
			returnString="y";
			return returnString;
		}else {
			returnString="n";
			return returnString;
		}
	}
	
	
	/**
	 * 根据编号删除入库信息
	 * @param inbId
	 */
	public void delInbound(String inbId) {
		iMapper.delInbound(inbId);
	}
	
	/**
	 * 修改入库信息
	 * @param inbound
	 */
	public void editInbound(HashMap map) {
		iMapper.editInbound(map);
	}
	
	/**
	 * 审核通过
	 * @param inbId
	 */
	public void pass(String inbId) {
		iMapper.pass(inbId);//修改审核
		Inbound inbound=iDao.findByinbId(inbId);//根据编号查询
		HashMap map=new HashMap<>();
		map.put("inbId", inbId);
		map.put("inbStatic", inbound.getInbStatic());
		int i=iMapper.exit(map);//判断
		if(i>0) {
			HashMap map1=new HashMap<>();
			map1.put("proId", inbound.getProduct().getProId());
			map1.put("warName", inbound.getWarName());
			int z=imMapper.findByproIdAndwarName(map1);//寻找符合条件的产品，判断是否存在
			System.out.println("z="+z);
			if (z>0) {
				int j1=imMapper.findForinvNum(map1).getInvNum();//查询符合条件产品的库存数量
				int j2=imMapper.findForinvNum(map1).getInvHeight();//查询符合条件产品的挤压库存预警数量
				int j3=imMapper.findForinvNum(map1).getInvLow();//获取符合条件产品的低库存预警数
				j1=j1+inbound.getInbNum();
				//判断在入库后是否还存在低库存状态
				if (j1>j3||j1<j2) {
					map1.put("invStatic", "正常");
					imMapper.editinvStatic(map1);//修改库存状态保持低库存状态
				}
				//判断是否为库存积压状态
				if (j1>j2) {
					map1.put("invStatic", "积压警告");
					imMapper.editinvStatic(map1);//修改库存状态，由正常改为库存积压
				}
				HashMap map2=new HashMap<>();
				map2.put("proId", inbound.getProduct().getProId());
				System.out.println("map2 proId="+inbound.getProduct().getProId());
				map2.put("invNum", j1);
				map2.put("warName", inbound.getWarName());
				imMapper.editinvNum(map2);
			}else {
				HashMap map2=new HashMap<>();
				map2.put("invId", "inv0009");
				map2.put("proId", inbound.getProduct().getProId());
				map2.put("warName", inbound.getWarName());
				map2.put("invUnit", inbound.getInbUnit());
				map2.put("invNum", inbound.getInbNum());
				System.out.println("新增的库存单位和数量: "+map.get("inbUnit")+"----"+map.get("inbNum"));
				map2.put("invLow",0);
				map2.put("invHeight",0);
				map2.put("invStatic", "正常");
				map2.put("invNote", "");
				//新增库存信息
				imMapper.addInventory(map2);
			}
		}
	}
}
